package com.yc.txx;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class WebConfig implements WebApplicationInitializer {


    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext cxt =
                new AnnotationConfigWebApplicationContext();
        cxt.register(MvcConfig.class);

        DispatcherServlet servlet = new DispatcherServlet(cxt);
        ServletRegistration.Dynamic registration =
                servletContext.addServlet("app", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/");

        // 文件上传临时文件
        MultipartConfigElement mc = new MultipartConfigElement(
                "/",
                1024*1024*1,
                1024*1024*10,
                1024*1204*5);
//        private String location; 临时文件夹
//        private long maxFileSize; 限制单个文件大小
//        private long maxRequestSize; 限制请求的整体大小(多文件上传)
//        private int fileSizeThreshold; 保存到临时文件的阈值,超过保存到临时文件
        registration.setMultipartConfig(mc);
    }
}
